package com.reform.service.impl;

import com.alibaba.fastjson.JSON;
import com.reform.pojo.expert.ExpertLabel;
import com.reform.mapper.ExpertLabelMapper;
import com.reform.res.Result;
import com.reform.res.ResultCode;
import com.reform.service.IExpertLabelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;

/**
 * <p>
 * 专家标签绑定 服务实现类
 * </p>
 *
 * @author mortale
 * @since 2021-08-13
 */
@Service
public class ExpertLabelServiceImpl extends ServiceImpl<ExpertLabelMapper, ExpertLabel> implements IExpertLabelService {

    @Autowired
    ExpertLabelMapper expertLabelMapper;

    @Override
    public Result creatExpertLabel(String expertLabels) throws SQLException {
        List<ExpertLabel> expertList;
        //解析JSON
        try {
            expertList = JSON.parseArray(expertLabels, ExpertLabel.class);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure(ResultCode.PARAM_JSON_IS_WRONG);
        }
        int res = 0;
        for (ExpertLabel expert : expertList) {
            res += expertLabelMapper.insert(expert);
        }
        if (res == expertList.size()) {
            return Result.success(ResultCode.SUCCESS);
        }
        return Result.failure(ResultCode.FAILURE);
    }

    @Override
    public Result deleteExpertLabel(String expertLabels) throws SQLException {
        List<ExpertLabel> expertList;
        //解析JSON
        try {
            expertList = JSON.parseArray(expertLabels, ExpertLabel.class);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure(ResultCode.PARAM_JSON_IS_WRONG);
        }
        int res = 0;
        for (ExpertLabel expert : expertList) {
            res += expertLabelMapper.deleteById(expert);
        }
        if (res == expertList.size()) {
            return Result.success(ResultCode.SUCCESS);
        }
        return Result.failure(ResultCode.FAILURE);
    }
}
